#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/statvfs.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include <sched.h>
#include <ctype.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <linux/if.h>

#define DEVTYPE_DISK 2
#define DEVTYPE_U    3

typedef struct _dev_param
{
    unsigned char devname[2];
    unsigned char devtype;   /* 2:harddisk, 3:u disk */
    unsigned char partition_count;
    unsigned int dev_totalspace;     /* KB */
    unsigned int partition_size[12]; /* KB */
} dev_param;

typedef struct _dev_info
{
    dev_param dev[8];
    int dev_count;
    int disk_num;
}dev_info;

int drv_cp_file(char * src_file, char * get_file, int find_string, char * string)
{
    int ret = 0;
    FILE * src_fp = NULL;
    FILE * get_fp = NULL;
    int length = 0;
    ssize_t size;
    size_t len = 0;
    char * p = NULL;
    char * line = NULL;

    if (access(src_file, F_OK) != 0) {
        printf("ERROR:src file is not exist!\n");
        return -1;
    }

    if (ret == 0) {
        src_fp = fopen(src_file, "r");
        get_fp = fopen(get_file, "w+");
        if ((get_fp != NULL) && (src_fp != NULL)) {
            while ((size = getline(&line, &len, src_fp)) != -1) {
                if (size > 0) {
                    if (find_string == 1) {
                        p = strstr(line, string);
                        if (p != NULL) {
                            length = fwrite(line, size, 1, get_fp);
                        }
                    }
                    else {
                        length = fwrite(line, size, 1, get_fp);
                    }
                }
            }

            fclose(src_fp);
            fclose(get_fp);

            if (line) 
                free(line);

            ret = 0;
        }
        else {
            if (src_fp) {
                printf("Error:open flie %s\n", get_file);
                fclose(src_fp);
            }

            if (get_fp) {
                printf("Error:open file %s\n", src_file);
                fclose(get_fp);
            }

            ret = -1;
        }
    }

    return ret;
}

int find_device(char * name)
{
    int xx = 1, i = 0, dev_type;
    ssize_t size;
    size_t len = 0;
    unsigned char k;
    dev_info devinfo;
    char temp_devname[12];
    char * line = NULL;
    char * pname;
    char * token;
    char seps[] = " ";
    FILE * fp = NULL;

    bzero(temp_devname, 12);
    bzero(&devinfo, sizeof(devinfo));
    drv_cp_file("/proc/partitions", "/tmp/partitions.txt", xx, "sd");
    fp = fopen("/tmp/partitions.txt", "r");

    /* get system harddisk and Mobile Device Summary Architecture */
    while((size = getline(&line, &len, fp)) != -1) {
        pname = strrchr(line, ' ');
        printf("pname = %s\n", pname);
        if(pname == NULL)
            continue;
        pname++;
        strcpy(name, pname);
        if(memcmp(pname, temp_devname, 3) == 0) { /* new partition */
            printf("new partition\n");
            i = 0; 
            token = strtok(line, seps);
            while (token != NULL) {
                if (i == 2) {
                    k = devinfo.dev[devinfo.dev_count - 1].partition_count;
                    devinfo.dev[devinfo.dev_count - 1].partition_size[k] = atoi(token);
                }
                i++;
                token = strtok(NULL, seps);
            }
            devinfo.dev[devinfo.dev_count - 1].partition_count++;
        }
        else { /* new device */
            printf("new device\n");
            memcpy(devinfo.dev[devinfo.dev_count].devname, pname, 3);
            i = 0;
            token = strtok(line, seps);
            while (token != NULL) {
                if (i == 2) {
                    devinfo.dev[devinfo.dev_count].dev_totalspace = atoi(token);
                }
                i++;
                token = strtok(NULL, seps);
            }
            devinfo.dev_count++;
        }
        memcpy(temp_devname, pname, 3);
    }

    printf("dev count = %d\n", devinfo.dev_count);


    return devinfo.dev_count;
}

int main(void)
{
	int count = 0;
	char name[12] = {0};

	count = find_device(name);
	printf("main device count = %d name = %s \n", count, name);

	return 0;
}





